const state = {
	openid: (() => {
		const savedOpenid = uni.getStorageSync('openid') || ''
		console.log('Initializing openid from storage:', savedOpenid)
		return savedOpenid
	})(),
	userInfo: uni.getStorageSync('userInfo') || null,
	PromotionId: uni.getStorageSync('PromotionId') || null,
	token: uni.getStorageSync('token') || ''
}

const mutations = {
	SET_OPENID(state, openid) {
		console.log('Setting openid:', openid)
		state.openid = openid
		uni.setStorageSync('openid', openid)
	},
	SET_PROMOTIONID(state, PromotionId) {
		state.PromotionId = PromotionId
		uni.setStorageSync('PromotionId', PromotionId)
	},
	SET_USERINFO(state, userInfo) {
		state.userInfo = userInfo
		uni.setStorageSync('userInfo', userInfo)
	},
	SET_TOKEN(state, token) {
		state.token = token
		uni.setStorageSync('token', token)
	},
	CLEAR_USER(state) {
		state.openid = ''
		state.userInfo = null
		state.token = ''
		uni.removeStorageSync('openid')
		uni.removeStorageSync('userInfo')
		uni.removeStorageSync('token')
	}
}

const getters = {
	getOpenid: state => state.openid
}

export default {
	namespaced: true,
	state,
	mutations,
	getters
}